package org.loofer.uiframework;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import org.loofer.ext.utils.SdkVersionUtils;
import org.loofer.uiframework.navigator.backstack.AbstractFragment;
import org.loofer.uiframework.navigator.backstack.FragmentBackStackManager;
import org.loofer.uikit.LayoutInflaterProvider;
import java.util.List;

public abstract class BaseFragment extends AbstractFragment implements LayoutInflaterProvider, ILoadFinished {
    private static final int ANIMATION_END_DELAY = 100;
    private static Drawable sGlobalBackgroundDrawable;
    private View mBusinessView;
    private LoadState mLoadState = LoadState.NONE;

    private enum LoadState {
        NONE,
        DOING_ANIM,
        WAITING_PARENT,
        FINISHED
    }

    public static Drawable getGlobalBackgroundDrawable() {
        return sGlobalBackgroundDrawable;
    }

    public static void setGlobalBackgroundDrawable(Drawable globalBackgroundDrawable) {
        sGlobalBackgroundDrawable = globalBackgroundDrawable;
    }

    public void onDestroy() {
        getFragmentBackHelper().clearChildFragmentBackStackManager();
        super.onDestroy();
    }

    public void onDestroyView() {
        super.onDestroyView();
    }

    public void onDetach() {
        setViewBackgroundDrawable(this.mBusinessView != null ? this.mBusinessView : getView(), null);
        super.onDetach();
        this.mBusinessView = null;
    }

    public void onResume() {
        super.onResume();
        if (this.mLoadState == LoadState.NONE && canLoadDataWhenResume()) {
            checkLoadFinished();
        }
    }

    public void onStart() {
        super.onStart();
    }

    public void onStop() {
        super.onStop();
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (sGlobalBackgroundDrawable != null) {
            setBackgroundDrawable(sGlobalBackgroundDrawable);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected void showToastMessage(String message) {
        Toast.makeText(getActivity(), message, 0).show();
    }

    protected void setBusinessView(View businessView) {
        this.mBusinessView = businessView;
    }

    public void setBackgroundDrawable(Drawable drawable) {
        if (this.mBusinessView != null) {
            setViewBackgroundDrawable(this.mBusinessView, drawable);
        }
    }

    @TargetApi(16)
    private void setViewBackgroundDrawable(View view, Drawable drawable) {
        if (view == null) {
            return;
        }
        if (SdkVersionUtils.hasJellyBean()) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }

    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        if (enter) {
            if (nextAnim != 0) {
                try {
                    Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
                    anim.setAnimationListener(new AnimationListener() {
                        public void onAnimationStart(Animation animation) {
                        }

                        public void onAnimationRepeat(Animation animation) {
                        }

                        public void onAnimationEnd(Animation animation) {
                            new Handler().postDelayed(BaseFragment$1$$Lambda$1.lambdaFactory$(this), 100);
                        }

                        private /* synthetic */ void lambda$onAnimationEnd$0() {
                            if (BaseFragment.this.mLoadState == LoadState.DOING_ANIM) {
                                BaseFragment.this.checkLoadFinished();
                            }
                        }
                    });
                    this.mLoadState = LoadState.DOING_ANIM;
                    return anim;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        onExitAnimationStart();
        return super.onCreateAnimation(transit, enter, nextAnim);
    }

    public boolean isLoadFinished() {
        return this.mLoadState == LoadState.FINISHED;
    }

    public void onLoadFinished() {
    }

    protected boolean canLoadDataWhenResume() {
        return true;
    }

    protected void onExitAnimationStart() {
    }

    private void onParentFragmentLaunchFinished() {
        if (this.mLoadState == LoadState.WAITING_PARENT) {
            checkLoadFinished();
        }
    }

    protected void checkLoadFinished() {
        if (isResumed() && getUserVisibleHint()) {
            BaseFragment parentFragment = (BaseFragment) getParentFragment();
            if (parentFragment == null || parentFragment.isLoadFinished()) {
                if (getFragmentBackHelper().getParentFragmentBackStackManager() != null) {
                    getFragmentBackHelper().getParentFragmentBackStackManager().hidePreviousFragment(this);
                }
                this.mLoadState = LoadState.FINISHED;
                onLoadFinished();
                List<Fragment> fragments = getChildFragmentManager().getFragments();
                if (fragments != null) {
                    for (Fragment fragment : fragments) {
                        if (fragment instanceof BaseFragment) {
                            ((BaseFragment) fragment).onParentFragmentLaunchFinished();
                        }
                    }
                    return;
                }
                return;
            }
            this.mLoadState = LoadState.WAITING_PARENT;
        }
    }

    public LayoutInflater requestLayoutInflater() {
        return getActivity().getLayoutInflater();
    }

    public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
        return getActivity().getLayoutInflater();
    }

    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (this.mLoadState == LoadState.NONE) {
            checkLoadFinished();
        }
    }

    public void hideInputMethod() {
        Activity activity = getActivity();
        if (activity != null) {
            View focusView = activity.getCurrentFocus();
            if (focusView != null) {
                ((InputMethodManager) activity.getSystemService("input_method")).hideSoftInputFromWindow(focusView.getWindowToken(), 0);
            }
        }
    }

    public void showInputMethod(EditText editText) {
        Activity activity = getActivity();
        if (activity != null && editText != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService("input_method");
            editText.requestFocus();
            inputMethodManager.showSoftInput(editText, 2);
        }
    }

    public boolean isTopFragment() {
        FragmentBackStackManager stackManager = getFragmentBackHelper().getParentFragmentBackStackManager();
        return stackManager != null && stackManager.getTopFragment() == this;
    }
}
